<template>
  <div class="td-patient-delection-test-item">
    <div class="test-item-list">
      <div v-for="(item,i) in testItemList" :key="i">
        <h2 @click="selectTestItemTypeHandler(item)">
          <label>
            <i
              class="fa"
              :class="{'fa-circle':item.check,'fa-circle-thin':!item.check}"
              aria-hidden="true"
            ></i>
          </label>
          <label>
            {{item.label}}：
            <span>({{item.gender}})</span>
          </label>
        </h2>
        <p v-for="(arg,j) in item.itemList" :key="j" @click="selectTestItemHandler(item,arg)">
          <span class="check">
            <i
              class="fa"
              :class="{'fa-circle':arg.check,'fa-circle-thin':!arg.check}"
              aria-hidden="true"
            ></i>
          </span>
          <span>{{arg.code}}</span>
          <span>{{arg.label}}</span>
          <span>{{arg.price}}元</span>
        </p>
      </div>
    </div>
  </div>
</template>

<script>
import testItemTypeService from "../../../../_services/testItemType.service";
import testItemService from "../../../../_services/testItem.service";
import testItemPackageService from "../../../../_services/testItemPackage.service";
import Enumerable from "linq";
import { mapActions } from "vuex";
export default {
  name: "TianDingPatientDelectionTestItemComponent",
  data() {
    return {
      testItemList: [],
      selectTestItemTypeList: [],
      selectTestItemList: [],
      selectTestItemPackageList: [],
      total: 0
    };
  },
  mounted() {
    this.selectTestItemPackageList = this.$store.state.item.packageList;
    this.selectTestItemTypeList = this.$store.state.item.testItemTypeList;
    this.selectTestItemList = this.$store.state.item.testItemList;
    this.getTestItemList();
  },
  methods: {
    getTestItemList() {
      this.indicator$.open("正在加载...");

      this.testItemList = [];

      let testItemTypePromise = testItemTypeService.getTestItemTypeList();
      let testItemPromise = testItemService.getTestItemList();

      this.http$.all([testItemTypePromise, testItemPromise]).then(
        this.http$.spread((testItemTypeResponse, testItemResponse) => {
          //对检测项目类型进行排序  根据排序值进行升序  再根据使用次数进行降序
          let testItemTypeList = Enumerable.from(testItemTypeResponse.Data)
            .orderBy(e => e.Sorting)
            .orderByDescending(e => e.UsedNum)
            .toArray();

          let testItemList = Enumerable.from(testItemResponse.Data).toArray();
          if (testItemTypeList.length !== 0) {
            testItemTypeList.forEach(element => {
              element.label = element.TestItemTypeName;
              element.value = element.TypeID;
              element.gender = element.IsMan === 0 ? "女" : "男";
              element.colorText = "黄";
              switch (element.TypeColor) {
                case 0:
                  element.colorText = "黄";
                  break;
                case 1:
                  element.colorText = "紫";
                  break;
                case 2:
                  element.colorText = "红";
                  break;
                case 3:
                  element.colorText = "绿";
                  break;
              }
              element.check = false;

              let check = this.selectTestItemTypeList.findIndex(
                e => e.value === element.value
              );
              element.check = check === -1 ? false : true;

              if (testItemList.length !== 0) {
                let testItemArray = testItemList.filter(
                  (item, index, array) => {
                    item.label = item.ItemName;
                    item.value = item.TestItemID;
                    item.code = item.ItemCode;
                    item.price = this.utils$.subTransfer(item.ItemPrice);
                    item.check = false;
                    item.gender = element.gender;

                    let ch = this.selectTestItemList.findIndex(
                      e => e.value === item.value
                    );
                    item.check = ch === -1 ? false : true;

                    return item.TestItemTypeID === element.TypeID;
                  }
                );

                element.itemList = Enumerable.from(testItemArray)
                  .orderBy(e => e.Sorting)
                  .orderByDescending(e => e.UsedNum)
                  .toArray();
              }
              if (element.itemList.length !== 0) {
                this.testItemList.push(element);
              }
            });
          }

          this.indicator$.close();
        })
      );
    },
    selectTestItemTypeHandler(item) {
      item.check = !item.check;
      item.itemList.filter(e => (e.check = item.check));

      //如果不包含则添加到已选择检测项目类型列表（-1为不包含）
      let index = this.selectTestItemTypeList.findIndex(
        e => e.value === item.value
      );

      if (item.check) {
        if (index === -1) {
          this.selectTestItemTypeList.push(item);
          this.selectTestItemList.push(...item.itemList);
        }
      } else {
        if (index !== -1) {
          this.selectTestItemTypeList.splice(index, 1);
          item.itemList.forEach(element => {
            let i = this.selectTestItemList.findIndex(
              e => e.value === element.value
            );
            this.selectTestItemList.splice(i, 1);
          });
        }
      }

      this.calcTotal();
    },
    selectTestItemHandler(item, arg) {
      arg.check = !arg.check;
      let itemList = item.itemList.filter(e => e.check);
      item.check = itemList.length === item.itemList.length;

      let index = this.selectTestItemList.findIndex(e => e.value === arg.value);

      if (arg.check) {
        if (index === -1) {
          this.selectTestItemList.push(arg);
        }
      } else {
        if (index !== -1) {
          this.selectTestItemList.splice(index, 1);
        }
      }

      let jndex = this.selectTestItemTypeList.findIndex(
        e => e.value === item.value
      );

      if (item.check) {
        if (jndex === -1) {
          this.selectTestItemTypeList.push(item);
        }
      } else {
        if (jndex !== -1) {
          this.selectTestItemTypeList.splice(jndex, 1);
        }
      }

      this.calcTotal();
    },
    calcTotal() {
      this.total = 0;
      if (this.selectTestItemList.length !== 0) {
        this.selectTestItemList.forEach(e => {
          this.total += parseFloat(e.price);
        });
      }

      let state = {
        selectTestItemTypeList: this.selectTestItemTypeList,
        selectTestItemList: this.selectTestItemList,
        selectTestItemPackageList: this.selectTestItemPackageList,
        total: this.total
      };
      this.testItemAction(state);
    },
    ...mapActions(["testItemAction"])
  }
};
</script>
